import { Component, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFRadioWidgetSchema, SFSchema, SFSchemaEnum, SFSelectWidgetSchema, SFUISchema } from '@delon/form';
import { ShipperBaseService } from '@shared';
import { NzMessageService } from 'ng-zorro-antd/message';
import { UsermanageService } from 'src/app/routes/usercenter/services/usercenter.service';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { map } from 'rxjs/operators';
import { of } from 'rxjs';

@Component({
  selector: 'app-ad-components-Sale',
  templateUrl: './editSale.component.html',
  styleUrls: ['./editSale.component.less']
})
export class EditSaleComponentsAddComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  record: any = {};
  i: any;
  sts: any;
  rows: any;
  schema: SFSchema = {};
  detailData: any = {};
  ui: SFUISchema = {};

  constructor(
    public msgSrv: NzMessageService,
    public service: UsermanageService,
    private modal: NzModalRef,
    public shipperservice: ShipperBaseService
  ) {}

  ngOnInit(): void {
    if (this.sts == '2') {
      this.initDetailData();
    }
    this.initSF();
  }
  initDetailData() {
    const params = {
      id: this.i.id
    };
    this.service.request(this.service.$api_partnerChannelUpdateDetaiList, params).subscribe(res => {
      if (res) {
        this.detailData = res;
      } else {
        this.service.msgSrv.error(res.msg);
      }
    });
  }
  initSF() {
    this.schema = {
      properties: {
        // channelId: {
        //   type: 'string',
        //   title: '渠道销售修改为',
        //   ui: {
        //     widget: 'select',
        //     placeholder: '请选择',
        //     allowClear: true,
        //     visibleIf: {
        //       _$expand: (value: boolean) => value
        //     },
        //     asyncData: () => this.shipperservice.getChanners()
        //   }
        // },
        channelId: {
          type: 'string',
          title: '渠道销售修改为',
          ui: {
            widget: 'select',
            serverSearch: true,
            searchDebounceTime: 300,
            searchLoadingText: '搜索中...',
            allowClear: true,
            onSearch: (q: any) => {
              let str = q.replace(/^\s+|\s+$/g, '');
              let params :any= {}
              if (str) {
                console.log(+str);
                if(+str) {
                  params.phoneNumber = str
                } else {
                  params.name = str
                }
                return this.service
                  .request(this.service.$api_get_channel, params)
                  .pipe(map((res: any) => (res as any[]).map(i => ({ label: `${i.name}/${i.telephone}`, value: i.id } as SFSchemaEnum))))
                  .toPromise();
              } else {
                return of([]);
              }
            },
          } as SFSelectWidgetSchema
        },
        remark: {
          type: 'string',
          title: '备注',
          ui: {
            widget: 'textarea',
            placeholder: '请不要超过50个字',
            maxLength: 50,
            autosize: { minRows: 2, maxRows: 6 }
          }
        },
        effectiveNode: {
          type: 'string',
          title: '生效节点',
          ui: {
            widget: 'radio',
            showRequired: true
          } as SFRadioWidgetSchema,
          enum: [
            { label: '修改成功后立即生效', value: 1 },
            // { label: 'CRM流程审核通过后生效', value: 2 },
          ],
        },
      },
      required: ['channelId', 'remark', 'effectiveNode']
    };
    this.ui = {
      '*': {
        spanLabelFixed: 180,
        grid: { span: 18 },
        width: 600
      }
    };
  }
  get reqParams() {
    return {};
  }
  save() {
    let enterId: any;
    if (this.sts == '1') {
      enterId = this.rows;
    } else {
      enterId = [this.i.id];
    }
    this.service.nzModalService.create({
      nzContent: '确定提交吗？',
      nzOnOk: () => {
        const params = {
          ...this.sf?.value,
          enterpriceIds: enterId
        };
        this.service.request(this.service.$api_batchUpdateEnterpriceChannel, params).subscribe(res => {
          if (res) {
            this.service.msgSrv.success('修改成功');
            this.modal.destroy(true);
          }
        });
      }
    });
  }

  close(): void {
    this.modal.close(true);
  }
}
